home *** CD-ROM | disk | FTP | other *** search
- /*
- ListInDialog
-
- A snippet that shows how to (uuuhhhhh) put a list in a dialog.
-
- I thought we had this one, but I guess not. It's easy, just create the list
- right after you create the dialog, then call LUpdate and LClick in a
- dialog filter to respond to user events.
-
- Please see the snippet DialogBits for a more comprehensive treatment of
- everything you can do in a dialog (well, almost).
-
- This is a 680x0 version, built with the Universal Headers.
-
- C.K. Haun
- April '94
- Are you sure we don't already have a snippet like this????
- Oh, I rememeber, that was on the //gs
-
- */
-
- #include <Dialogs.h>
- #include <Controls.h>
- #include <QuickDraw.h>
- #include <Windows.h>
- #include <ToolUtils.h>
- #include <OSUtils.h>
- #include <Menus.h>
- #include <Fonts.h>
- #include <resources.h>
- #include <Sound.h>
- #include <Traps.h>
- #include <Gestaltequ.h>
- #include <Memory.h>
- #include <Scrap.h>
- #include <TextEdit.h>
- #include <Lists.h>
-
- enum {
- kSampleDialog = 128
- };
- enum {
- kListItem = 3
- };
-
- pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
-
- void SetUpList(ListHandle theList);
-
- /* making the list handle a global */
- ListHandle gAList;
-
- main()
- {
-
- // this "rect" defines the data bounds of the list. In this case, a
- // one column list
- Rect listRect2 = {
- 0, 0, 0, 1
- };
- // cell to initialize with
- Cell cp = {
- 0, 0
- };
-
- // the dialog we're using
- DialogPtr myDialog = nil;
-
- // hitItem for ModalDialog call
- short hitItem = 0;
-
- // these three are here for all the GetDItem/SetDItem calls
- Rect tempRect;
- short tempItem;
- Handle tempHandle;
-
- // a temporary boolean (now THAT'S a helpful comment!)
- Boolean tempBool;
-
- /* start up managers */
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- /* get our dialog. It is created HIDDEN, and shown after I set the */
- /* user item that will hold the list */
-
- myDialog = GetNewDialog(kSampleDialog, nil, (WindowPtr)-1);
-
- // setting it up in the Dialog manager's records,
- GetDItem(myDialog, kListItem, &tempItem, &tempHandle, &tempRect);
-
- /* inset the rect by 16, which is the width of the scroll bar that will be attached */
- /* to this list */
- tempRect.right -= 16;
-
- /* set the current port to this dialog */
- SetPort(myDialog);
-
- /* create the list */
-
- gAList = LNew(&tempRect, // in the tempRect bounds
- &listRect2, // with a sinngle column
- cp, // default cell size (a cell of 0,0 says that)
- nil, // no special LDEF, use the standard one
- myDialog, // put it in this port
- false, // do NOT draw initially
- false, // does NOT have a grow box space
- false, // does NOT scroll horizontally
- true); // DOES have a verticle scroll bar
-
- // call another funtion to put in the strings
- SetUpList(gAList);
-
- // turn drawing on after the list has been filled
- LDoDraw(true, gAList);
-
- // show the dialog
- ShowWindow((WindowPtr)myDialog);
-
- /* draw it once */
- DrawDialog(myDialog);
-
- // loop until ModalDialog is done
- do {
- // we have to use a ModalDialog filter, since ModalDialog doesn't
- // automatically handle lists
- ModalDialog((ModalFilterProcPtr)theListFilter, &hitItem);
-
- // switch off what item was hit
- switch (hitItem) {
- /* I don't care about any of the hits here */
-
- }
- // wait for an OK or Cancel
- }
- while (hitItem != ok && hitItem != cancel);
-
- // don't do anything in this sample
- DisposeDialog(myDialog);
- }
-
-
- /* Here is the filter that handles any List activity */
- pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
- {
-
- Rect tempR;
- Boolean returnValue = false; // defaults to me not saying I handled anything
- Boolean theBoolean;
- WindowPtr oldP;
-
- // get the current port, set the port to this dialog
- GetPort(&oldP);
- SetPort(theDialog);
-
- // was this an update event for this dialog?
- if (theEvent->what == updateEvt && theEvent->message == theDialog) {
-
-
- // Update the list.
- LUpdate(theDialog->visRgn, gAList);
-
- // get the list rectangle
- tempR = (*gAList)->rView;
-
- // push it outwards one pixel and frame the list
- InsetRect(&tempR, -1, -1);
- FrameRect(&tempR);
-
- // NOTE: Do !NOT! return 'true' if you did SOME drawing in response to a dialog update
- // event in your filter!
- // ONLY if you did EVERYTHING should you return 'true', or else you can cause other updating
- // not to occur, which would be Bad
-
- } else {
- // see if this was a mouseDown event
- if (theEvent->what == mouseDown) {
- Point theP;
-
- // we set the port to the dialog on entry to the filter, so a GetMouse will work
- GetMouse(&theP);
-
- // get the list rectangle
- tempR = (*gAList)->rView;
-
- // add the scroll bar back in for hit testing (remember we took it out earlier)
- tempR.right += 16;
-
- // See if they clicked in our list!
- if (PtInRect(theP, &tempR)) {
- theBoolean = LClick(theP, nil, gAList);
- // if they double-clicked the list, return 1, as if the OK button had been pressed
- if (theBoolean)
- *itemHit = 1;
- else
- *itemHit = kListItem;
-
- // tell the Dialog Manager that we handled this click, it can stop searching for a click-owner
- returnValue = true;
- }
- }
- }
-
- // reset the original port
-
- SetPort(oldP);
-
- return(returnValue);
- }
-
-
- /* simple function to put a list of strings into my list */
- void SetUpList(ListHandle theList)
- {
- register qq;
- Cell cp;
- Str255 theString;
- short listCount = 0;
- Boolean Changed = false;
- short theCount;
- Handle tempHandle = GetResource('STR#', 128);
- if(tempHandle){
- theCount = *((short *)(*tempHandle));
- if (theCount) {
- for (qq = 0; qq < theCount; qq++) {
- GetIndString(theString, 128, qq + 1);
- listCount = LAddRow(1, listCount + 1, theList);
- cp.h = 0;
- cp.v = listCount;
- LAddToCell(&theString[1], theString[0], cp, theList);
- }
- }
- }
- }
-